package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.pojo.UserInfo;
import com.itheima.service.UserDiagramService;
import com.itheima.vo.AgeVO;
import com.itheima.vo.GenderVO;
import com.itheima.vo.IndustryVO;

import com.itheima.vo.UserDistributionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class UserDiagramServiceImpl implements UserDiagramService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 封装数据
     */
    @Override
    public UserDistributionVO distribution() {
        UserDistributionVO userDistribution = new UserDistributionVO();
        userDistribution.setIndustryDistribution(getTrade());
        userDistribution.setGenderDistribution(getSex());
        userDistribution.setAgeDistribution(getAge());
        return userDistribution;
    }

    /**
     * 查询行业的数量
     *
     * @return
     */
    @Override
    public  List<IndustryVO> getTrade() {

//        HashMap<String, Integer> tradeSize = new HashMap<>();

        List<IndustryVO> industryVOList = new ArrayList<>();
        IndustryVO industryVOComputer = new IndustryVO();
        //行业分布 计算机行业,金融行业,机械行业,医疗行业
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("industry", "计算机行业");
        Integer iTNum = this.userInfoMapper.selectCount(queryWrapper);
        industryVOComputer.setValue(iTNum);
        industryVOComputer.setName("计算机行业");
        industryVOList.add(industryVOComputer);

        IndustryVO industryVOFinance = new IndustryVO();
        QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("industry", "金融行业");
        Integer financialNum = this.userInfoMapper.selectCount(queryWrapper1);
        industryVOFinance.setValue(financialNum);
        industryVOFinance.setName("金融行业");
        industryVOList.add(industryVOFinance);

        IndustryVO industryVOMechanics = new IndustryVO();
        QueryWrapper<UserInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("industry", "机械行业");
        Integer mechanicalNum = this.userInfoMapper.selectCount(queryWrapper2);

        industryVOMechanics.setValue(mechanicalNum);
        industryVOMechanics.setName("机械行业");
        industryVOList.add(industryVOMechanics);

        IndustryVO industryVOMedicalCare = new IndustryVO();
        QueryWrapper<UserInfo> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("industry", "医疗行业");
        Integer medicalNum = this.userInfoMapper.selectCount(queryWrapper3);
//        tradeSize.put("医疗行业", medicalNum);
        industryVOMedicalCare.setValue(mechanicalNum);
        industryVOMedicalCare.setName("医疗行业");
        industryVOList.add(industryVOMedicalCare);

        return industryVOList;
    }

    /**
     * 查询男女的数量
     *
     * @return
     */
    @Override
    public List<GenderVO> getSex() {
//        HashMap<String, Integer> sexSize = new HashMap<>();

        List<GenderVO> genderVOList = new ArrayList<>();
        GenderVO manVO = new GenderVO();
        QueryWrapper<UserInfo> queryWrapperMan = new QueryWrapper<>();
        queryWrapperMan.eq("sex", "1");
        Integer manCount = userInfoMapper.selectCount(queryWrapperMan);
        manVO.setValue(manCount);
        manVO.setName("男");
        genderVOList.add(manVO);



        GenderVO womanVO = new GenderVO();
        QueryWrapper<UserInfo> queryWrapperWoMan = new QueryWrapper<>();
        queryWrapperWoMan.eq("sex", "2");
        Integer womanCount = userInfoMapper.selectCount(queryWrapperWoMan);
        womanVO.setValue(womanCount);
        womanVO.setName("女");
        genderVOList.add(womanVO);

        return genderVOList;
    }

    /**
     * 获取年龄分布数据
     *
     * @return
     */
    @Override
    public  List<AgeVO> getAge() {
//        HashMap<String, Integer> ageSize = new HashMap<>();

        List<AgeVO> ageVOList = new ArrayList<>();
        AgeVO firstVO = new AgeVO();
        QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lt("age", 20);
        Integer firstValue = userInfoMapper.selectCount(queryWrapper1);
        firstVO.setValue(firstValue);
        firstVO.setName("0~19");
        ageVOList.add(firstVO);
        AgeVO secondVO = new AgeVO();

        QueryWrapper<UserInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lt("age", 30).gt("age", 19);
        Integer secondValue = userInfoMapper.selectCount(queryWrapper2);
        secondVO.setValue(secondValue);
        secondVO.setName("20~29");
        ageVOList.add(secondVO);

//        ageSize.put("", );
        AgeVO thirdVO = new AgeVO();
        QueryWrapper<UserInfo> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.lt("age", 40).gt("age", 29);
        Integer thirdValue = userInfoMapper.selectCount(queryWrapper3);
        thirdVO.setValue(thirdValue);
        thirdVO.setName("30~39");
        ageVOList.add(thirdVO);

        AgeVO fourthVO = new AgeVO();
        QueryWrapper<UserInfo> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.lt("age", 50).gt("age", 39);
        Integer fourthValue = userInfoMapper.selectCount(queryWrapper4);
        fourthVO.setValue(fourthValue);
        fourthVO.setName("40~49");
        ageVOList.add(fourthVO);
        AgeVO fifthVO = new AgeVO();
        QueryWrapper<UserInfo> queryWrapper5 = new QueryWrapper<>();
        queryWrapper5.gt("age", 49);
        Integer fifthValue = userInfoMapper.selectCount(queryWrapper5);
        fifthVO.setValue(fifthValue);
        fifthVO.setName(">50");
        ageVOList.add(fifthVO);

        return ageVOList;

    }

}
